/*
 * MailBean.java
 * 
 */
package util;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
    
    private Mail() {
    }
    
    public static boolean sendMail(String to, String subject, String text) {
        // TODO: change it to the JNDI Lookup
        String userName = "customer_service@2handeasy.com";
        String paword = "1qaz2WSX";
        String host = "smtp.gmail.com";
        Properties props = new Properties();
        props.put("mail.smtp.user", userName);
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        if (false) {
            props.put("mail.smtp.debug", "true");
        } else {
            props.put("mail.smtp.debug", "false");
        }
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");

        try {
            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(true);
            MimeMessage msg = new MimeMessage(session);
            msg.setText(text);
            msg.setSubject(subject);
            msg.setFrom(new InternetAddress("customer_service@2handeasy.com"));

            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

            msg.saveChanges();
            Transport transport = session.getTransport("smtp");
            transport.connect(host, userName, paword);
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
            return true;
        } catch (Exception mex) {
            //mex.printStackTrace();
            return false;
        }
    }
}
